#include "Game.h"

Game::Game()
{
    loadBackground();
}

Game::~Game()
{
    //dtor
}
int Game::menu()
{


    ITEM** items_menu;
    MENU* menu;
    int c,retour;
    Evenement::init();
    char *choix[3] = {
                        "Jouer",
                        "Options",
                        "Quitter",
                  };

    unsigned int n=sizeof(choix)/sizeof(choix[0]);
    items_menu=(ITEM**)calloc(n+1,sizeof(ITEM*));
    int taille[4]={n+2,12,0,0};
    taille[2]=(NY-taille[0])/2;
    taille[3]=(NX-taille[1])/2;
    WINDOW * win = newwin(taille[0], taille[1] ,taille[2] , taille[3]);
    for(int i=0;i<n;i++)
        items_menu[i]=new_item(choix[i],NULL);
    items_menu[n]=NULL;
    box(win, 0, 0);
    menu=new_menu(items_menu);
    set_menu_win(menu, win);
    set_menu_sub(menu,derwin(win, taille[0]-1, taille[1]-1, 1, 1));
    post_menu(menu);
    wrefresh(win);

    keypad(stdscr, TRUE);
    while((c = getch())!= ' ' )
	{

        if(c==Evenement::getBas())
            menu_driver(menu, REQ_DOWN_ITEM);
        else if(c==Evenement::getHaut())
            menu_driver(menu, REQ_UP_ITEM);

		wrefresh(win);
	}
    keypad(stdscr, FALSE);

    retour=item_index(current_item(menu));
    unpost_menu(menu);
	for(int i=0;i<n+1;i++)
        free_item(items_menu[i]);
	free_menu(menu);

	wrefresh(win);
	return retour;
}
void Game::gererMenu()
{

    c_=new GameManager();
    int r=1;
    drawBackground();
    refresh();
    while(r!=0){
        r=menu();
        if(r==1)
            options();
        if(r==2)
            exit(0);
        Affichage::effaceTout();
        drawBackground();
        refresh();
    }
    c_->setPerso(selecPerso());
    c_->Start();

}
void Game::options()
{
    ITEM** items_menu;
    MENU* menu;
    int c,retour;
    Evenement::init();
    string touche(Evenement::getTouchePossible());

    bool continuer =true;
    char *choix[8] = {
                        "Monter ",
                        "Aller a droite ",
                        "Descendre ",
                        "Aller a gauche ",
                        "Prendre un escalier ",
                        "Cheat Vision ",
                        "Tirer ",
                        "Retour"
                  };

    unsigned int n=sizeof(choix)/sizeof(choix[0]);
    char t[2*n];
    for(int i=0;i<n;i++){
        t[2*i]=touche[i];
        t[2*i+1]='\0';  //SINON cela affiche le restant de la chaine de caractères on est onc obliger d'insérer une fin de chaine
    }
    int taille[4]={n+2,25,0,0};
    taille[2]=(NY-taille[0])/2;
    taille[3]=(NX-taille[1])/2;
    WINDOW * win = newwin(taille[0], taille[1] ,taille[2] , taille[3]);
    items_menu=(ITEM**)calloc(n+1,sizeof(ITEM*));
    for(int i=0;i<n;i++){
        items_menu[i]=new_item(choix[i],&t[2*i]);
    }
    box(win, 0, 0);
    items_menu[n]=NULL;
    menu=new_menu(items_menu);
    set_menu_win(menu, win);
    set_menu_sub(menu,derwin(win, taille[0]-1, taille[1]-1, 1, 1));

    post_menu(menu);
    wrefresh(win);
    keypad(stdscr, TRUE);
    while(continuer)
	{
	    c = getch();

            if(c==Evenement::getBas())
                menu_driver(menu, REQ_DOWN_ITEM);
            else if(c==Evenement::getHaut())
                menu_driver(menu, REQ_UP_ITEM);
            else if(c== ' '){
                retour=item_index(current_item(menu));
                if(retour!=(n-1)){
                    t[2*retour]=getch();
                    touche[retour]=t[2*retour];
                    items_menu[retour]=new_item(choix[retour],&(t[2*retour]));
                    post_menu(menu);
                    wrefresh(win);
                }else
                    continuer=false;
            }

		wrefresh(win);
	}
    keypad(stdscr, FALSE);
    unpost_menu(menu);
	for(int i=0;i<n+1;i++)
        free_item(items_menu[i]);
	free_menu(menu);
	Evenement::setTouchePossible(touche);
	refresh();

}
int Game::selecPerso()
{


    ITEM** items_menu;
    MENU* menu;
    int c,retour;
    Evenement::init();
    char *choix[3] = {
                        "Jedi",
                        "Combattant",
                  };

    unsigned int n=sizeof(choix)/sizeof(choix[0]);
    items_menu=(ITEM**)calloc(n+1,sizeof(ITEM*));
    int taille[4]={n+2,12,0,0};
    taille[2]=(NY-taille[0])/2;
    taille[3]=(NX-taille[1])/2;
    WINDOW * win = newwin(taille[0], taille[1] ,taille[2] , taille[3]);
    for(int i=0;i<n;i++)
        items_menu[i]=new_item(choix[i],NULL);
    items_menu[n]=NULL;
    box(win, 0, 0);
    menu=new_menu(items_menu);
    set_menu_win(menu, win);
    set_menu_sub(menu,derwin(win, taille[0]-1, taille[1]-1, 1, 1));
    post_menu(menu);
    wrefresh(win);

    keypad(stdscr, TRUE);
    while((c = getch())!= ' ' )
	{
	    if(c==Evenement::getBas())    //On monte ou on descend
            menu_driver(menu, REQ_DOWN_ITEM);
        else if(c==Evenement::getHaut())
            menu_driver(menu, REQ_UP_ITEM);
		wrefresh(win);
	}
    keypad(stdscr, FALSE);

    retour=item_index(current_item(menu));
    unpost_menu(menu);
	for(int i=0;i<n+1;i++)
        free_item(items_menu[i]);
	free_menu(menu);

	wrefresh(win);
	return retour;
}
void Game::loadBackground()
{
    ifstream ffichier("T-fighter.csv");
    stringstream buffer;
    buffer << ffichier.rdbuf();

    back_=buffer.str();



}
void Game::drawBackground()
{
    Affichage::affiche(back_);



}

